#ifndef TCP_TCPSERVER_H
#define TCP_TCPSERVER_H

#include "Acceptor.h"
#include <map>
#include <memory>

class TcpConnection;

typedef std::shared_ptr<TcpConnection> TcpConnectionPtr;
typedef std::function<void(const TcpConnectionPtr &)> ConnectionCallback;
typedef std::function<void(const TcpConnectionPtr &, const char *, ssize_t)> MessageCallback;

class TcpServer{
public:
    TcpServer(const struct sockaddr_in & listenAddr);
    ~TcpServer();

    void start();

    void setConnectionCallback(const ConnectionCallback & cb){
        connectionCallback_ = cb;
    }

private:
    void newConnection(int sockfd, const struct sockaddr_in & peerAddr);

    typedef std::map<std::string, TcpConnectionPtr> ConnectionMap;
    std::shared_ptr<Acceptor> acceptor_;
    ConnectionCallback connectionCallback_;
    ConnectionMap  connections_;
    // 持有当前存货的TcpConnection 的shar_ptr指针,
};

#endif //TCP_TCPSERVER_H
